package com.zhifa.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhifa.Util.ComboboxUtil;
import com.zhifa.Util.DateJsonValueProcessor;
import com.zhifa.Util.DateUtil;
import com.zhifa.Util.FormatItems;
import com.zhifa.Util.ItemsToSalaryItem;
import com.zhifa.Util.ResponseUtil;
import com.zhifa.entity.DateTime;
import com.zhifa.entity.Items;
import com.zhifa.entity.PageBean;
import com.zhifa.entity.Salaryitem;
import com.zhifa.entity.Salarystandard;
import com.zhifa.service.SalaryitemService;
import com.zhifa.service.SalarystandardService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

@Controller
@RequestMapping("/salarystandard")
public class SalarystandardController {
	
	@Resource
	SalarystandardService salarystandardService;
	
	@Resource
	SalaryitemService salaryitemService;
	

	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
		//yyyy-MM-dd HH:mm:ss
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		dateFormat.setLenient(false);  
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值，false:不能为空值
	}

	@RequestMapping("/get")
	public String getstandarByid(@RequestParam(value="id") Integer id,HttpServletResponse response) throws Exception{
		
		Salarystandard salarystandard = salarystandardService.findsalarysByid(id);
		JSONArray jsonArray=new JSONArray();
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("id", salarystandard.getId());
		jsonObject.put("text", salarystandard.getName());
		jsonArray.add(jsonObject);
		ResponseUtil.write(response, jsonArray);
		return null;
	}
	
	@RequestMapping("/getsalarystandard")
	public String getsalarystandard(HttpServletResponse response) throws Exception{
		List<Salarystandard> all = salarystandardService.getAll();
		JSONArray salarystandard = ComboboxUtil.getSalarystandard(all);
		ResponseUtil.write(response, salarystandard);
		return null;
	}
	
	
	@RequestMapping("/addSalarystandard")
	public String addSalarystandard(Items items,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Salarystandard salarystandard=new Salarystandard(items.getTimestamp(), items.getName(), items.getRegistrar(), items.getRegisttime(), items.getTotal());
		List<Salaryitem>list=ItemsToSalaryItem.itemsToSalaryItem(items);
		boolean addStandardAndItems = salarystandardService.addStandardAndItems(salarystandard, list);
		  JSONObject result=new JSONObject();
			if(addStandardAndItems){ 
				result.put("success", true);
			}else{
				result.put("success", false);
			}
		ResponseUtil.write(response, result);
		return null;
	}
	
	

	@RequestMapping("/updateSalarystandard")
	public String updateSalarystandard(Items items,HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		Salarystandard salarystandard=new Salarystandard(items.getTimestamp(), items.getName(), items.getRegistrar(), items.getRegisttime(), items.getTotal());
		List<Salaryitem>list=ItemsToSalaryItem.itemsToSalaryItem(items);
		
		int updateSalarystandard = salarystandardService.updateSalarystandard(salarystandard, list);
		  JSONObject result=new JSONObject();
			if(updateSalarystandard>0){ 
				result.put("success", true);
			}else{
				result.put("success", false);
			}
		ResponseUtil.write(response, result);
		return null;
	}
	
	
	@RequestMapping("/getSalarystandardMap")
	public String getSalarystandardMap(DateTime dateTime,@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,HttpServletRequest request,HttpServletResponse response)throws Exception{
		String name = request.getParameter("name");
		String state = request.getParameter("state");
		PageBean pageBean=new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object>map=new HashMap<String, Object>();
		map.put("name", name);
		map.put("state", state);
		map.put("starttime",DateUtil.formatDate(dateTime.getStarttime(), "yyyy-MM-dd HH:mm:ss"));
		map.put("endtime", DateUtil.formatDate(dateTime.getEndtime(), "yyyy-MM-dd HH:mm:ss"));
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Salarystandard> listByMap = salarystandardService.getListByMap(map);
		List<Items> items=new ArrayList<Items>();
		for (int i = 0; i <listByMap.size(); i++) {
			Items tempitem=new Items();
			
			Salarystandard salarystandard = listByMap.get(i);
			tempitem.setTimestamp(salarystandard.getTimestamp());
			tempitem.setName(salarystandard.getName());
			tempitem.setTotal(salarystandard.getTotal());
			tempitem.setRegistrar(salarystandard.getRegistrar());
			tempitem.setRegisttime(salarystandard.getRegisttime());
			tempitem.setState(salarystandard.getState());
			
			
			String timestamp = salarystandard.getTimestamp();
			List<Salaryitem> salaryitems = salaryitemService.findSalaryitemByTimestamp(timestamp);
			//当前tempitem，添加salaryitems
			FormatItems.change(tempitem, salaryitems);
			items.add(tempitem);
			
		}
		
		Long countByMap = salarystandardService.getCountByMap(map);
		
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
		JSONArray jsonArray=JSONArray.fromObject(items,jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", countByMap);
		ResponseUtil.write(response, result);
		return null;
	}
	
	@RequestMapping("/getSalarystandardName")
	public String getSalarystandardName(HttpServletRequest request,HttpServletResponse response)throws Exception{
		List<Salarystandard>list=salarystandardService.getAll();
		JSONArray salarystandardJsonArray = ComboboxUtil.getSalarystandard(list);
		ResponseUtil.write(response, salarystandardJsonArray);
		return null;
	}
	
	@RequestMapping("/setSalarystandard")
	public String reviewSalarystandard(Items items,HttpServletResponse response) throws Exception{
		String timestamp = items.getTimestamp();
		int reviewSalarystandard = salarystandardService.setSalarystandard(timestamp);
		  JSONObject result=new JSONObject();
			if(reviewSalarystandard>0){ 
				result.put("success", true);
			}else{
				result.put("success", false);
			}
		ResponseUtil.write(response, result);
		return null;
	}
}
